﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Reflection;
using log4net;

namespace Cn.Edwon.Web {
    public class HttpUtil:System.Web.UI.Page {
        public static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public static CookieContainer myCookieContainer;
        

        /// <summary>
        /// 模拟Post提交，并返回提交的结果页信息
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postString">a=1&b=2形式的数据</param>
        /// <param name="encode">编码</param>
        /// <returns></returns>
        public static string ParsePostAction(string url, String contentType, string postString, Encoding encode) {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            byte[] requestBytes = Encoding.GetEncoding("gb2312").GetBytes(postString);
            req.Method = "post";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = requestBytes.Length;
            //req.AllowAutoRedirect = false;
            if (myCookieContainer == null) myCookieContainer = new CookieContainer();
            req.CookieContainer = myCookieContainer;

            Stream requestStream = req.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Close();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            res.Cookies = myCookieContainer.GetCookies(req.RequestUri);

            StreamReader sr = new StreamReader(res.GetResponseStream(), encode);
            string backstr = sr.ReadToEnd();
            sr.Close();
            res.Close();
            return backstr;
        }
        /// <summary>
        /// 具有缺省ContentType的Post提交
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postString"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
         public static string ParsePostAction(string url, string postString, Encoding encode) {
            return ParsePostAction(url, "application/x-www-form-urlencoded", postString, encode);
        }
        /// <summary>
        /// 具有SOAP 的contentType的POST提交
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postString"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
         public static string ParseSoapPostAction(string url, string postString, Encoding encode) {
            return ParsePostAction(url, "application/soap+xml", postString, encode);
        }
        /// <summary>
        /// 模拟Get提交，并返回提交的结果页信息
        /// </summary>
        /// <param name="rUrl"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string ParseGetAction(string rUrl, Encoding encode) {

            // used to build entire input
            StringBuilder sb = new StringBuilder();

            // used on each read operation
            byte[] buf = new byte[8192];

            // prepare the web page we will be asking for
            HttpWebRequest request = (HttpWebRequest)
                WebRequest.Create(rUrl);

            // execute the request
            HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();

            // we will read data via the response stream
            Stream resStream = response.GetResponseStream();

            string tempString = null;
            int totalcount = 0;
            int count = 0;
            do {
                // fill the buffer with data
                count = resStream.Read(buf, 0, buf.Length);

                // make sure we read some data
                if (count != 0) {
                    // translate from bytes to ASCII text
                    tempString = encode.GetString(buf, 0, count);
                    // continue building the string
                    sb.Append(tempString);
                }

                totalcount += count;
            }
            while (count > 0); // any more data to read?

            resStream.Close();

            return sb.ToString();

        }

    }
}
